<?php

// R0910CMS 
// created by TLightSky
// time : 20100224
// editBy:Guoanqi
// time : 20100331

if(!defined("IN_R0910CMS")) {
    exit("Access Denied!");
}

class R_Db_SQLMaker_Select extends R_Db_SQLMakerAbstract {
    public $tableName = null;
    public $what      = null;
    public $where     = null;
    public $limit     = null;
    public $order     = null;//array('id'=>'DESC')

    public function makeSQL() {
        $this->checkNotNull(array(
            'tableName', 'what', 'where',
            ) //end of the array
        );

        $t1 = "SELECT ";
        if(is_array($this->what)) {
            $t1.= join(' ,', array_map(array(__CLASS__, 'addSlash1'),$this->what));
        } else if(is_string($this->what)) {
            $t1.= $this->what; 
        } else {
            throw new R_Db_Exception('What should be a string or an arrary.');
        }

        $t2 = " FROM ".$this->addSlash1($this->tableName);
        $t3 = $this->makeWhere($this->where);

        $t4 = '';
        if(!empty($this->order)) {
            $t41 = $t42 = '';
            //print_r($this->order);
            if(is_string($this->order)) {
                $t41 = $this->addSlash1($this->order);
            } else 
            if(is_array($this->order)) {
            	foreach($this->order as $k=>$v){
            		$k=$this->addSlash1($k);
            		$t42[] = join(' ',array($k,$v));
            	}
            	$t41 = join(',',$t42);
                //$t41 = $this->addSlash1($this->order[0]);
                /*if(!empty($this->order[1])) {
                    $t42 = $this->order[1];
                }*/
            } else {
                throw new R_Db_Exception('Order should be a string or an arrary.');
            }

            $t4 = " ORDER BY {$t41} ";
        }

        $t5 = '';
        if(!empty($this->limit)) {
            $t5 = $this->makeLimit($this->limit);
        }

        $this->_sql = join("",array($t1,$t2,$t3,$t4,$t5,';',));
        //echo $this->_sql;
        return $this->_sql;
    }

}
